package com.tianshi.pxx.service.impl;

import com.tianshi.pxx.mapper.CartMapper;
import com.tianshi.pxx.mapper.OrdersMapper;
import com.tianshi.pxx.pojo.entity.Cart;
import com.tianshi.pxx.pojo.entity.Orders;
import com.tianshi.pxx.pojo.entity.Product;
import com.tianshi.pxx.pojo.vo.ProductVO;
import com.tianshi.pxx.service.CartService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service
@Slf4j
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
//    @Cacheable(value = "cart", key = "#userId")
    public void add(Integer productId, Integer userId) {
        //1. 查询当前用户是否已经购买过该商品
        //清楚redis缓存
        redisTemplate.delete(deleteKeys("cart*"));

        //2. 通过用户id查询当前商品id是否在购物车中
        Cart cart=cartMapper.selectByProductIdAndUserId(productId,userId);
        if(cart==null){ //3. 如果没有购买过该商品，则新增一条记录
            cartMapper.insert(new Cart(null,productId,userId,1));
        }else{ //4. 如果已经购买过该商品，则数量+1
            cart.setCount(cart.getCount()+1);
            cartMapper.update(cart);
        }
    }

    @Override
    @Cacheable(value = "cart-all", key = "#userId")
    public List<ProductVO> list(Integer userId) {
        //1. 通过用户id查询购物车中的所有商品id
        //2. 通过商品id查询商品信息
        //清楚redis缓存
//        redisTemplate.delete(deleteKeys("cart*"));

        //双表查询
        //select p.name,p.price,c.count from cart c,product p where c.product_id=p.id and c.user_id=1;

        List<ProductVO> cartList=cartMapper.selectProductVOByUserId(userId);
        log.info("CartServiceImpl.list  cartList:{}",cartList);
        return cartList;
    }

    @Override
    public void delete(Integer id) {
        redisTemplate.delete(deleteKeys("cart*"));
        //1. 通过id删除购物车中的记录
        cartMapper.delete(id);
    }

    @Override
    public void clear(Integer userId) {
        //清楚redis缓存
        redisTemplate.delete(deleteKeys("cart*"));
        //1. 通过用户id删除购物车中的所有记录
        cartMapper.deleteByUserId(userId);

    }

    @Override
    public void addCount(Integer id) {
        //清楚redis缓存
        redisTemplate.delete(deleteKeys("cart*"));
        Cart cart=cartMapper.selectById(id);
        cart.setCount(cart.getCount()+1);
        cartMapper.update(cart);
    }

    @Override
    public void subCount(Integer id) {
        //清楚redis缓存
        redisTemplate.delete(deleteKeys("cart*"));
        Cart cart=cartMapper.selectById(id);
        cart.setCount(cart.getCount()-1);
        if(cart.getCount()<=0){ //如果数量减到0，则删除该记录
            cartMapper.delete(id);
        }
        else cartMapper.update(cart); //否则更新数量
    }


    /*
    *
    *
    * */
    @Override
    public void checkout(Integer userId) {
        //清楚redis缓存
        redisTemplate.delete(deleteKeys("cart*"));
        redisTemplate.delete(deleteKeys("order*"));

        //获取cart表中所有商品id
        List<Integer> productIds= cartMapper.selectProductIdByUserId(userId);

        if(productIds!=null && productIds.size()>0){
            List<Orders> ordersList=new ArrayList<>(); //订单列表
            for(Integer productId:productIds){ //遍历商品id
                Orders orders=new Orders(null,productId,userId);
                ordersList.add(orders); //将商品id插入到订单列表中
            }
            ordersMapper.insertOrdersList(ordersList); //将购物车中的商品id插入到订单表中
            cartMapper.deleteByUserId(userId); //清空购物车
        }
    }

    private Set<String> deleteKeys(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        return keys;
    }


}
